package com.maven.rabbitmq.topic;

import com.maven.rabbitmq.constant.Constant;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class TopicComsumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1. 建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("8.135.54.49");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("rabbitmq");

        Connection connection = connectionFactory.newConnection();
        //2. 创建信道
        Channel channel = connection.createChannel();

        channel.queueDeclare(Constant.TOPIC_QUEUE_1,true,false,false,null);


        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("topic 收到的信息 ： " + new String(body));
            }
        };
        channel.basicConsume(Constant.TOPIC_QUEUE_1,true,consumer);

//        channel.close();
//        connection.close();

    }
}
